package com.quanshu.shop.limit.view;

import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.FrameLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.gyf.barlibrary.ImmersionBar;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.shop.limit.adapter.LImiteMainTitleAdapter;
import com.quanshu.shop.limit.entity.LimiteTitleEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class LimiteActivity extends BaseActivity {

    @BindView(R.id.title)
    ConstraintLayout title;
    @BindView(R.id.title_select)
    RecyclerView titleSelect;
    @BindView(R.id.limit_body)
    FrameLayout limitBody;
    @BindView(R.id.back)
    FrameLayout back;
    private List<LimiteTitleEntity> limiteTitleEntityList;
    private LImiteMainTitleAdapter lImiteMainTitleAdapter;
    private HashMap<Integer, Fragment> fragmentHashMap;
    private FragmentManager fragmentManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_limite);
        ButterKnife.bind(this);
        initView();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        ImmersionBar.with(this).destroy();
    }

    private void initView() {
        ImmersionBar.with(this).statusBarColor(R.color.color_FF5050).titleBar(title).init();
        RxViewHelper.getInstance().clicks(back, aVoid -> {
            finish();
        });

        fragmentManager = getSupportFragmentManager();
        limiteTitleEntityList = new ArrayList<>();
        fragmentHashMap = new HashMap<>();
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 2);
        titleSelect.setLayoutManager(gridLayoutManager);

        LimiteTitleEntity limiteTitleEntity = new LimiteTitleEntity();
        limiteTitleEntity.setTitle("限时抢购");
        limiteTitleEntityList.add(limiteTitleEntity);
        LimiteTitleEntity limiteTitleEntity1 = new LimiteTitleEntity();
        limiteTitleEntity1.setSelelct(false);
        limiteTitleEntity1.setTitle("限时秒杀");
        limiteTitleEntityList.add(limiteTitleEntity1);

        Limit1Fragment limit1Fragment = Limit1Fragment.newInstance();
        fragmentHashMap.put(0, limit1Fragment);
        Limit2Fragment limit2Fragment = Limit2Fragment.newInstance();
        fragmentHashMap.put(1, limit2Fragment);
        fragmentManager.beginTransaction().add(R.id.limit_body, fragmentHashMap.get(0), String.valueOf(0)).add(R.id.limit_body, fragmentHashMap.get(1), String.valueOf(1)).commit();
        lImiteMainTitleAdapter = new LImiteMainTitleAdapter(limiteTitleEntityList);
        lImiteMainTitleAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.limit_title_txt:
                        setTitleState(position);
                        break;
                }
            }
        });
        titleSelect.setAdapter(lImiteMainTitleAdapter);
        setTitleState(0);
    }

    private void setTitleState(int position) {
        for (int a = 0; a < limiteTitleEntityList.size(); a++) {
            limiteTitleEntityList.get(a).setSelelct(false);
            if (!fragmentHashMap.get(a).isHidden()) {
                fragmentManager.beginTransaction().hide(fragmentHashMap.get(a)).commit();
            }


        }
        limiteTitleEntityList.get(position).setSelelct(true);
        fragmentManager.beginTransaction().show(fragmentHashMap.get(position)).commit();
        lImiteMainTitleAdapter.notifyDataSetChanged();
    }
}
